//cookie
function Cookie() { };
Cookie.prototype.set = function (key, value, expires) {
    var data = new Date();
    data.setSeconds(data.getSeconds() + expires);
    document.cookie = key + '=' + value + ';expires=' + data.toUTCString();
}
Cookie.prototype.get = function (key) {
    var cookie = document.cookie;
    var arr = cookie.split('; ');
    var obj = {};
    arr.forEach(item => {
        var str = item.replace(/=/, '*a*')
        var arr = str.split('*a*')
        obj[arr[0]] = arr[1]
    })
    return obj[key]
}
Cookie.prototype.remove = function (key) {
    this.set(key, '', -1)
}
//搜索框
var q = new Cookie()
$('.head_search > input').val(q.get('q'))
//界面渲染
var src
var search = location.hash.substring( 3 , location.hash.length)
$.get('http://127.0.0.1:8000/search?q=' + search,{},function(data){
    if(data.data.length == 0){
        $('.product_no').css('display','block')
    }else{
        for(let i = 0 ; i < data.data.length; i++ ){
            console.log(data.data.length);
            var Product = $('.product_li_clone').clone()
            Product.removeClass('product_li_clone')
            Product.css('display','block')
            var src = data.data[i].urls.substring(data.data[i].urls.indexOf('h'),data.data[i].urls.indexOf('.jpg')+4)
            $(Product[0].children[0]).attr({'title':data.data[i].title,'src':src})  
            $('.product_ul').append(Product)
            $('.money').eq(i+1).text('￥' + data.data[i].price)
            $('.product_introduce').eq(i+1).text(data.data[i].title)
            // $('.bargain_num').eq(i+1).text(data.data[i])   //月成交代码
        }
    }
})  
//搜索
$('.search_btn').click(function () {
    var search = $('.head_search > input').val();
    q.set('q', search)
    location.href = 'http://127.0.0.1:5500/practise/mzsc_new/html/Product.li.html#q=' + $('.head_search > input').val();
    location.reload()
    $.get('http://127.0.0.1:8000/search?q=' + search,{},function(data){
        if(data.data.length == 0){
            $('.product_no').css('display','block')
        }else{
            for(let i = 0 ; i < data.data.length ; i++ ){
                var Product = $('.product_li_clone').clone()
                Product.removeClass('product_li_clone')
                Product.css('display','block')
                var src = data.data[i].urls.substring(data.data[i].urls.indexOf('h'),data.data[i].urls.indexOf('.jpg')+4)
                $(Product[0].children[0]).attr({'title':data.data[i].title,'src':src})
                $('.product_ul').append(Product)
                $('.money').eq(i+1).text('￥' + data.data[i].price)
                $('.product_introduce').eq(i+1).text(data.data[i].title)
                // $('.bargain_num').eq(i+1).text(data.data[i])   //月成交代码
            }
        }
    })
});
